<template>
	<div></div>
</template>
<script>
import { mapGetters, mapState, mapActions } from 'vuex'
import { getWxQrCodeQuery, getMiniAppUrl } from '@/api/common'
import { checkLoginFn } from '@/utils/auth'
export default {
	data() {
		return {}
	},
	computed: {
		...mapGetters({ userInfo: 'getBasicUser' })
	},
	onLoad(options) {
		uni.showLoading({
			title: '加载中...'
		})
		// 微信推送进入
		if (options.redirect_url) {
			this._getRedirectUrl(options)
			return false
		}
		// 扫码进入
		if (options.scene) {
			this._getWxQrCodeQuery(options.scene)
			return false
		}
		this.compatible()
	},
	methods: {
		// 扫码进入
		_getWxQrCodeQuery(scene) {
			getWxQrCodeQuery({ scene: scene })
				.then(res => {
					uni.hideLoading()
					if (res.data.url.indexOf('https://') > -1) {
						const returnUrl = encodeURIComponent(res.data.url)
						uni.navigateTo({
							url: `/pages/page/webview/index?returnUrl=${returnUrl}`
						})
					} else {
						uni.redirectTo({
							url: res.data.url
						})
					}
				})
				.catch(e => {
					this.compatible()
				})
		},
		// 微信推送进入
		// 该来源为推送的页面需要验证登录
		_getRedirectUrl(option) {
			// if (option.push_source == 'back') {
			let token = uni.getStorageSync('token')
			let redirectUrl = decodeURIComponent(option.redirect_url)
			uni.hideLoading()
			if (token) {
				checkLoginFn().then(res => {
					if (res) {
						let user_id = uni.getStorageSync('user_id')
						// console.log(this.$util.spliceUrl(`/${redirectUrl}`, `owner_user_id=${user_id}`))
						redirectUrl = this.$util.spliceUrl(`${redirectUrl}`, `owner_user_id=${user_id}`)
						// redirectUrl = redirectUrl.indexOf('/') == 0 ? redirectUrl : `/${redirectUrl}`
						uni.redirectTo({
							url: redirectUrl
						})
					} else {
						this.jumpLogin(redirectUrl)
					}
				}).catch(() => {
					this.jumpLogin(redirectUrl)
				})
			} else {
				this.jumpLogin(redirectUrl)
			}
			// }
		},
		// 兼容，如果小程序码或者接口返回有误时进入登录页
		compatible() {
			uni.hideLoading()
			uni.redirectTo({
				url: `/pages/page/login/index`
			})
		},
		// 跳转到登录页
		jumpLogin(url) {
			// uni.setStorageSync('loginReturnUrl', getMiniAppUrl())
			uni.setStorageSync('loginReturnUrl', url)
			uni.reLaunch({
				url: '/pages/page/login/index'
			})
		}
	}
}
</script>
